home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / newpap16.zip / NEWPAPER.C next >
C/C++ Source or Header  |  1991-08-27  |  9KB  |  222 lines

  1. /*
  2.  **************************************************************
  3.  * newpaper.c                                                 *
  4.  * 7/28/90 by Jim Button and Roger Hadgraft                   *
  5.  *------------------------------------------------------------*
  6.  * Updates WIN.INI Wallpaper specification to point to the    *
  7.  *   to the next BMP file in the windows subdirectory.        *
  8.  *                                                            *
  9.  * This hardly looks like a Windows program. The traditional  *
  10.  *   message loop is not even needed, since it does no input, *
  11.  *   has no output, and terminates almost before it starts!   *
  12.  **************************************************************
  13.  
  14. 16/10/1990  Looks in Win.ini for tiling or centering.
  15.         Otherwise it asks, and records the setting.
  16.         Pressing "Cancel" causes no setting to be recorded, and
  17.         sets centering on as a means of previewing the bitmap.
  18.  
  19. 16/11/1990  This version uses ZIP files instead of BMP files, and spawns
  20.         PKUNZIP to unpack the required file. Each bitmap is stored in a
  21.         ZIP file of the same name as itself. (eg. FRED.BMP is stored as FRED.ZIP).
  22.  
  23. 19/11/1990  Fixed (?) problem where a UAE would occur if the dialog box was produced.
  24.         It's suspected that this was caused by using the active window's handle.
  25.  
  26. 23/11/1990  Moved the execution of PKUZIP to the last action trying to fix the
  27.         hanging problem when the message box is generated.
  28.  
  29. 22/06/1991    Added a dialog box to ask if wallpaper should be changed.
  30.             Added capability for bitmaps to be stored in another directory.
  31.  
  32. 13/07/1991    Moved settings into NEWPAPER.INI from WIN.INI.
  33.             This file must be in the Windows directory.
  34.  
  35. 27/08/1991    Added the ability to run WUNZIP instead of PKUNZIP.
  36.             Added check to make sure bitmap file exists.
  37.  
  38. */
  39.  
  40. #ifndef _WINDOWS
  41. #define _WINDOWS
  42. #endif
  43.  
  44. #include <windows.h>
  45. #include <dos.h>
  46. #include <string.h>
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49.  
  50. static char NewPaper[] = "NewPaper 1.6 by Roger Hadgraft"
  51.             , NewPaperIni[] = "NewPaper.Ini";
  52.  
  53. #define MAX_PROF_STR  80
  54.  
  55. #include <..\subs\chcurdir.c>
  56.  
  57. int PASCAL WinMain(  HANDLE hInstance,
  58.                      HANDLE hPrevInstance,
  59.                      LPSTR lpszCmdLine,
  60.                      int nCmdShow)
  61.     {
  62.     unsigned    int done;
  63.     char    oldbmp[MAX_PROF_STR+1];
  64.     char    firstbmp[MAX_PROF_STR+1];
  65.     char    newbmp[MAX_PROF_STR+1];
  66.     char    wallpaperdir[MAX_PROF_STR+1];
  67.     char    unzipper[MAX_PROF_STR+1];
  68.     char    windowsdir[145];
  69.     char    cmdline[200];
  70.     FILE    *fBmpfile;
  71.     HWND    hWnd;
  72.  
  73.    struct   find_t dta;
  74. /* struct   stat   buf; */
  75.    char     *tiled;
  76.    int      result, iShowFlag, confirm, i;
  77.  
  78.    /* create a window as parent for the message boxes that are created */
  79.    hWnd = CreateWindow( (LPSTR)"NewPaper",
  80.                         (LPSTR)NewPaper,
  81.                         WS_OVERLAPPED,
  82.                         100,      /*  x - ignored for tiled windows */
  83.                         100,      /*  y - ignored for tiled windows */
  84.                         50,       /* cx - ignored for tiled windows */
  85.                         20,       /* cy - ignored for tiled windows */
  86.                         (HWND)NULL,        /* no parent */
  87.                         (HMENU)NULL,       /* use class menu */
  88.                         (HANDLE)hInstance, /* handle to window instance */
  89.                         (LPSTR)NULL        /* no params to pass on */
  90.                         );
  91.  
  92.     /* find current bitmap */
  93.     confirm = GetPrivateProfileInt((LPSTR)"Options", (LPSTR)"Confirm", 1, (LPSTR)NewPaperIni );
  94.  
  95.     /* check if any change is to be made */
  96.     if( confirm == 1 )
  97.         result = MessageBox( hWnd, (LPSTR)"Change the BitMap on the Desktop ?",
  98.             NewPaper, MB_ICONQUESTION | MB_YESNO );
  99.  
  100.     if( result == IDYES || confirm == 0 ) {
  101.  
  102.         /* find current bitmap in WIN.INI */
  103.         GetProfileString((LPSTR)"Desktop", (LPSTR)"WallPaper", (LPSTR)"", (LPSTR)oldbmp, MAX_PROF_STR);
  104.         /* change to the Windows directory, just in case the program was started from somewhere else */
  105.         GetWindowsDirectory( windowsdir, 145 );
  106.         ChangeCurDir( windowsdir );
  107.  
  108.         /* check for a bitmap directory in NEWPAPER.INI */
  109.         GetPrivateProfileString((LPSTR)"Options", (LPSTR)"WallPaperDir", (LPSTR)"", (LPSTR)wallpaperdir, MAX_PROF_STR, (LPSTR)NewPaperIni );
  110.         if( wallpaperdir[0] != NULL )    /* then change to the relevant directory */
  111.             ChangeCurDir( wallpaperdir );
  112.  
  113.         strcpy( strstr( oldbmp, ".BMP" ), ".ZIP" );
  114.         newbmp[0] = '\0';                            /* In case no BMP files exist */
  115.  
  116.         /*** Get the list of .ZIP files ***/
  117.         done = _dos_findfirst("*.ZIP", 0, &dta);     /* Find files */
  118.         if (!done) {
  119.             strcpy(firstbmp, dta.name);
  120.             strcpy(newbmp, firstbmp);
  121.             }
  122.  
  123.         while (!done) {
  124.             if (!strcmpi(dta.name, oldbmp)) {         /* Current one? */
  125.                  done = _dos_findnext(&dta);
  126.                 strcpy(newbmp, (done ? firstbmp : dta.name));
  127.                 break;
  128.                 }
  129.             done = _dos_findnext(&dta);
  130.             }
  131.  
  132.         if( newbmp[0] == NULL ) {    /* couldn't find any ZIP files */
  133.             MessageBox( hWnd, 
  134.                 "Couldn't find any Zipped files.\nDesktop bitmap was not changed.\nCheck the WallpaperDir= line in NEWPAPER.INI.", NewPaper, MB_OK );
  135.             return( FALSE );
  136.             }
  137.  
  138.         /* change to the Windows directory, just in case the program was started from somewhere else */
  139.         ChangeCurDir( windowsdir );
  140.         /* delete the old BMP file here */
  141. /*        MessageBox( hWnd, oldbmp, NewPaper, MB_OK );    debug only */
  142.         strcpy( strstr( oldbmp, ".ZIP" ), ".BMP" );
  143.         remove( oldbmp );
  144.  
  145.         /* build the command line to later spawn PKUNZIP or WUNZIP to unzip the file */
  146.         GetPrivateProfileString((LPSTR)"Options", (LPSTR)"Unzipper", (LPSTR)"pkunzip", (LPSTR)unzipper, MAX_PROF_STR, (LPSTR)NewPaperIni );
  147.         if( strcmpi( unzipper, "WUNZIP" ) == 0 ) {
  148.             strcpy( cmdline, "wunzip.exe " );
  149.             if( wallpaperdir[0] != NULL ) {        /* then give a full path name */
  150.                 strcat( cmdline, wallpaperdir );
  151.                 strcat( cmdline, "\\" );
  152.                 }
  153.             strcat( cmdline, newbmp );        /* this is actually a .ZIP name */
  154.             strcat( cmdline, " -p" );        /* unzip into the Windows directory */
  155.             strcat( cmdline, windowsdir );
  156.             strcat( cmdline, " -a -e -o -g" );
  157.             }
  158.         else {    /* assume PKUNZIP */
  159.             strcpy( cmdline, "pkunzip.pif " );
  160.             if( wallpaperdir[0] != NULL ) {        /* then give a full path name */
  161.                 strcat( cmdline, wallpaperdir );
  162.                 strcat( cmdline, "\\" );
  163.                 }
  164.             strcat( cmdline, newbmp );        /* this is actually a .ZIP name */
  165.             strcat( cmdline, " ." );        /* unzip into the current (Windows) directory */
  166.             }
  167. /*         MessageBox( hWnd, cmdline, NewPaper, MB_OK ); debug only */
  168.  
  169.         /* run Unzipper */
  170.         if( ( GetWinFlags() & WF_ENHANCED ) == WF_ENHANCED )
  171.             iShowFlag = SW_SHOWMINIMIZED;   /* run as an icon in enhanced mode */
  172.         else
  173.             iShowFlag = SW_SHOW;
  174.         if( WinExec( (LPSTR)cmdline, iShowFlag ) < 32 )
  175.             MessageBox( hWnd, (LPSTR)"Problem running PKunZip", (LPSTR)"NewPaper", MB_ICONEXCLAMATION | MB_YESNOCANCEL );
  176.  
  177.         /* now replace .ZIP with .BMP */
  178.         strcpy( strstr( newbmp, ".ZIP" ), ".BMP" );
  179.  
  180.         /*** Update WIN.INI ***/
  181.         WriteProfileString((LPSTR)"Desktop", (LPSTR)"WallPaper", (LPSTR)newbmp);
  182.  
  183.         /*** Check for a previously recorded setting, otherwise ask ***/
  184.         result = GetPrivateProfileInt("Bitmaps", newbmp, -1, (LPSTR)NewPaperIni );
  185.         if( result == -1 ) {
  186.             /* this bitmap hasn't been specified yet */
  187.             result = MessageBox( hWnd, (LPSTR)"Tile this BitMap on the DeskTop ?\n(Otherwise centre it)",
  188.                 (LPSTR)newbmp, MB_ICONQUESTION | MB_YESNOCANCEL );
  189.             if( result == IDYES )
  190.                    tiled = "1";                         /* Point to string "1" */
  191.             else
  192.                 tiled = "0";
  193.             if( result != IDCANCEL )
  194.                    /* record choice as a permanent setting */
  195.                 WritePrivateProfileString((LPSTR)"Bitmaps", (LPSTR)newbmp, (LPSTR)tiled, (LPSTR)NewPaperIni);
  196.             }
  197.  
  198.         else {
  199.             if( result == 1 )
  200.                 tiled = "1";                         /* Point to string "1" */
  201.             else
  202.                 tiled = "0";
  203.                }
  204.  
  205.         /*** Update WIN.INI ***/
  206.         WriteProfileString((LPSTR)"Desktop", (LPSTR)"TileWallpaper", (LPSTR)tiled);
  207.  
  208.         /* check that the bitmap file exists */
  209.         for( i=0; i<50; i++ ) Yield();    /* wait for the unzipper to finish */
  210.         if( ( fBmpfile = fopen( newbmp, "r" ) ) == NULL ) {
  211.             /* the file doesn't exist! */
  212.             MessageBox( hWnd, "The bitmap file doesn't exist!\nPlease make sure that the bitmap and zip files\nshare the same name.\nThe WallPaper= setting has been cleared.", NewPaper, MB_OK );
  213.             WriteProfileString((LPSTR)"Desktop", (LPSTR)"WallPaper", (LPSTR)"");
  214.             }
  215.         else 
  216.             fclose( fBmpfile );
  217.  
  218.         }
  219.  
  220.     return( FALSE );
  221.     }
  222.